// pages/createAccount/createAccount.js
Page({
  data: {
    name: '',
    icon: '',
    deicon:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2Fc7c4907e-2ad6-494a-bf23-3eebb2918047%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1749804980&t=ddd6fd1dc8e667655b9549450f77fe0a',
    balance: 0,
    remark: '',
    uploading: false,
    loginde: false,
  },

  onNameInput(e) {
    this.setData({name: e.detail.value})
  },

  onIconInput(e) {
    this.setData({icon: e.detail.value})
  },

  onRemarkInput(e) {
    this.setData({remark: e.detail.value})
  },

  onBalanceInput(e) {
    this.setData({balance: e.detail.value})
  },

  chooseImage() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      success: (res) => {
        this.uploadImage(res.tempFilePaths[0])
      }
    })
  },

  uploadImage(tempFilePath) {
    this.setData({uploading: true})
    const cloudPath = `account/${Date.now()}-${Math.random().toString(36).substr(2, 8)}.jpg`
    
    wx.cloud.uploadFile({
      cloudPath,
      filePath: tempFilePath,
      success: res => {
        this.setData({icon: res.fileID})
        wx.showToast({title: '上传成功'})
      },
      fail: err => {
        console.error('上传失败:', err)
        wx.showToast({title: '上传失败', icon: 'none'})
      },
      complete: () => this.setData({uploading: false})
    })
  },

  onSubmit() {

    if(this.data.loginde){
      wx.showToast({title: '操作频繁', icon: 'none'})
      return
    }

    this.setData({
      loginde: true
    })

    if (!this.data.name.trim()) {
      wx.showToast({title: '请填写名称', icon: 'none'})
      return
    }

    if (this.data.uploading) {
      wx.showToast({title: '图片上传中', icon: 'none'})
      return
    }

    wx.cloud.callFunction({
      name: 'saveAccount',
      data: {
        icon: this.data.icon== null?this.data.deicon:this.data.icon,
        name: this.data.name,
        balance: this.data.balance,
        remark: this.data.remark,
        after: 0.0
      },
      success: res => {
        console.log(res)
        if(this.data.balance !== 0){
          wx.cloud.callFunction({
            name: 'saveAccountDetail',
            data: {
              accountId: res.result.data._id,
              date: new Date().toISOString().split('T')[0], // 格式化为年-月-日
              type: this.data.balance > 0? 0:1,
              amount: Math.abs(this.data.balance), // 使用绝对值
              remark: '初始金额'
            },
            success: res => {
              console.log('默认计入:',res)
            },
            fail: err => {
              console.error('保存失败:', err)
              wx.showToast({title: '保存失败', icon: 'none'})
            }
          })
        }
        wx.showModal({
          title: '记账成功',
          content: `${this.data.name} 已记录！`,
          showCancel: false,
          success: () => wx.navigateBack({detail:1})
        })
      },
      fail: err => {
        console.error('保存失败:', err)
        wx.showToast({title: '保存失败', icon: 'none'})
      }
    })
  }
})